package com.pm.main.domain.model;

import com.pm.main.application.model.CustomerValue;
import lombok.AllArgsConstructor;
import lombok.Setter;

import java.time.LocalDateTime;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

/**
 * 团
 */
public class Group {
    private Long id;
    private  BlockingQueue<GroupMember> groupMembers;


    /**
     * 拼团价格
     */
    @Setter
    private Double price;
    private GroupSpecification specification;


    public Group join(CustomerValue customer) {

        boolean success = !specification.expire() && groupMembers.add(GroupMember.of(customer.getId(), customer.getName()));
        if (!success) {
            throw new RuntimeException("该团已经失效");
        }
        return this;
    }

    /**
     * 团成员
     */

    @AllArgsConstructor
    static class GroupMember {

        /**
         * 顾客唯一标识
         */
        private final Long id;
        /**
         * 顾客名称
         */
        private final String name;

        public static GroupMember of(Long id, String name) {
            return new GroupMember(id, name);
        }
    }

    /**
     * 团规范
     */
    static class GroupSpecification {
        /**
         * 截止日期
         */
        private LocalDateTime dateline;
        /**
         * 人数限制
         */
        private Integer limit;

        /**
         * 是否失效
         *
         * @return true失效
         */
        public boolean expire() {
            return LocalDateTime.now().compareTo(dateline) > 0;
        }

    }


}